package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingPlanVo> getAllNursingPlan() {
        List<NursingPlanVo> nursingPlanVos = nursingPlanMapper.getAllNursingPlan();
        return nursingPlanVos;
    }

    @Override
    @Transactional
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();

//       将dto中的属性拷贝到实体类中
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setStatus(1);
        //将id返回取出

         nursingPlanMapper.insert(nursingPlan);
         Long id = nursingPlan.getId();
         log.info(id+"");


        //dto中的项目计划取出来
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        //判断集合是否为空
        if (projectPlans != null && projectPlans.size() != 0) {
            projectPlans.forEach(o -> {
                o.setPlanId(id);
                nursingProjectPlanMapper.insert(o);
            });
        }
    }

    @Override
    public PageResponse page(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> page0 = nursingPlanMapper.selectByPage(name, status);

        if (page0 != null && page0.size() > 0) {
            page0.forEach(o -> {
                //设置绑定被护理等级绑定的个数
                o.setCount(nursingLevelMapper.selectCountByPlanId(o.getId()));
            });
        }
        return PageResponse.of(page0, NursingPlan.class);
    }

    @Override
    public NursingPlanVo searchById(Long id) {
        //查询出计划信息
        log.info("查询计划信息");
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        Long planId = nursingPlanVo.getId();
        //计划信息中加入项目计划信息,查询该计划的项目计划信息然后加入计划中
        //根据计划id查询出全部项目计划信息
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectByPlanId(planId);

        nursingPlanVo.setProjectPlans(nursingProjectPlans);
        log.info(nursingProjectPlans+"");
        return nursingPlanVo;
    }

    @Transactional
    @Override
    public void delete(Long id) {
        nursingPlanMapper.delete(id);
        nursingProjectPlanMapper.deleteByPlanId(id);
    }

    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlanMapper.update(nursingPlan);
        //删除原来的护理计划关系数据
        nursingProjectPlanMapper.deleteByPlanId(nursingPlanDto.getId());
        //添加新的护理计划关系数据
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        //将planId设置到projectPlans中
        projectPlans.forEach(o -> {
            o.setPlanId(nursingPlanDto.getId());
            nursingProjectPlanMapper.insert(o);
        });
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(id);
        nursingPlan.setStatus(status);
        nursingPlanMapper.update(nursingPlan);
    }


}
